/************************************************************************
 * The code is owned by mainframer.cn and Shang Tao
 * Without aproval of mainframer.cn and Shang Tao, removing the copyright
 * infomation is regarded as invassion of proprietary copyright
 ***********************************************************************/
package com.mr.qa.dao.impl;

import java.util.List;
import java.util.Set;

import junit.framework.TestCase;

import org.hibernate.Transaction;

import com.mr.qa.bo.QuestionCategory;
import com.mr.qa.util.HibernateSessionFactory;

public class QuestionCategoryDAOTest extends TestCase {



	public QuestionCategoryDAOTest(String name) {
		super(name);
	}

	protected void setUp() throws Exception {
		super.setUp();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testSave() {
		QuestionCategoryDAO categoryDAO = new QuestionCategoryDAO();
		Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();

			QuestionCategory category = new QuestionCategory();
			category.setName("n2");
			category.setState("1");
			
			categoryDAO.save(category);
			
			QuestionCategory category2 = new QuestionCategory();
			category2.setName("n3");
			category2.setState("2");
			category2.setMrIaskCategory(category);
			
			categoryDAO.save(category2);
			
			tx.commit();

		} catch (RuntimeException re) {
			re.printStackTrace();
		} finally {
			if (tx != null) {
				tx.rollback();
			}
		}
	}

	public void testDelete() {
		fail("Not yet implemented");
	}

	public void testFindById() {
		QuestionCategoryDAO categoryDAO = new QuestionCategoryDAO();
		QuestionCategory category = categoryDAO.findById(new Integer(4));
		Set set = category.getMrIaskCategories();
		QuestionCategory category2 = (QuestionCategory)set.iterator().next();
		assertEquals("n3", category2.getName());
	}

	public void testFindAll() {
		QuestionCategoryDAO categoryDAO = new QuestionCategoryDAO();
                System.out.println("categoryDAO.findAll().size()="+categoryDAO.findAll().size());
		assertEquals(5, categoryDAO.findAll().size());
	}

	public void testFindByExample() {
		fail("Not yet implemented");
	}

	public void testFindByProperty() {
		QuestionCategoryDAO categoryDAO = new QuestionCategoryDAO();
		/*List list = categoryDAO.findAll();
		
		QuestionCategory categoryParent = null;
		
		for(int i = 0; i < list.size(); i++){
			QuestionCategory category = (QuestionCategory)list.get(i);
			if(category.getMrIaskCategory() != null){
				categoryParent = category.getMrIaskCategory();
				//assertEquals(4, categoryParent.getId().intValue());
				break;
			}
		}
		
		List list2 = categoryDAO.findByProperty("mrIaskCategory", categoryParent);
		assertEquals(1,list2.size());*/
		List list = categoryDAO.findAll();
		for(int i = 0; i < list.size(); i++){
			QuestionCategory category = (QuestionCategory)list.get(i);
			if(category.getMrIaskCategory() == null){
			list.remove(i);
			i--;
			}
			//System.out.println();
		}
		
		assertEquals(4,list.size());
		
	}

	public void testFindByName() {
		fail("Not yet implemented");
	}

	public void testFindByState() {
		fail("Not yet implemented");
	}

	public void testMerge() {
		fail("Not yet implemented");
	}

	public void testAttachDirty() {
		fail("Not yet implemented");
	}

	public void testAttachClean() {
		fail("Not yet implemented");
	}

}
